package com.zkingsofter.service;

import java.sql.Connection;

import javax.sql.XADataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import com.zkingsoft.model.Dictionary;


public class DBUtil {
	private static final String DRIVER="com.mysql.jdbc.Driver";
	
	static{
		try {
			Class.forName(DRIVER);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static XADataSource createXADataSource(Dictionary dict) throws Exception {  
        MysqlXADataSource dataSource = new MysqlXADataSource();  
        dataSource.setURL("jdbc:mysql://"+dict.getIp()+":3306/"+dict.getName());  
        dataSource.setUser(dict.getUserName());  
        dataSource.setPassword(dict.getPassword()); 
        return dataSource;
    }  
  
	public static  void closeConnection(Connection  conn){
		try {
			if(conn!=null&&!conn.isClosed()){
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}  
}
